package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.dao.GoodsProductDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.GoodsProductDO;
import com.bootdo.wx.domain.MemberStockDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.domain.OrderOrderDO;
import com.bootdo.wx.service.MemberStockService;
import com.bootdo.wx.service.OrderOrderService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.StringUtils;
/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-07-07 18:44:13
 */
 
@Controller
@RequestMapping("/wx/memberStock")
public class MemberStockController {
	@Autowired
	private MemberStockService memberStockService;
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderOrderService orderOrderService;
	
	
	@GetMapping("/{id}")
	@RequiresPermissions("wx:memberStock:memberStock")
	String MemberStock(@PathVariable("id") String id,Model model){
		model.addAttribute("userId", id);
	    return "wx/memberStock/memberStock";
	}
	
	@GetMapping("/getStockHistory/{productId}/{userId}")
	@RequiresPermissions("wx:memberStock:memberStock")
	String getStockHistory(@PathVariable("productId") String productId,@PathVariable("userId") String userId,Model model){
		model.addAttribute("productId", productId);
		model.addAttribute("userId", userId);
		return "wx/memberStock/memberStockHistory";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberStock:memberStock")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","memberStock");
	    params.put("order","desc");
	    params.put("flag", "1");
        //查询列表数据
	    Query query = new Query(params);
	    R memberStockList2 = memberStockService.getMemberStockList(query);
	    List<Map<String, Object>> mapList =(List<Map<String, Object>>)memberStockList2.get("data");
	    if(mapList.size()>0){
	    	for (Map<String, Object> map : mapList) {
	    		List<GoodsProductDO> list = goodsProductDao.list(map);
	    		if(list.size()>0){
	    			MemberUserDO memberUserDO = memberUserDao.get(params.get("userId").toString());
	    			if(memberUserDO!=null&&memberUserDO.getLevel()==1)
	    				map.put("price", list.get(0).getPriceC());
	    			else
	    				map.put("price", list.get(0).getPriceC());
	    		}
			}
	    }
       
	   /* if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<MemberStockDO> memberStockList = memberStockService.likeList(query);
            int total = memberStockList.size();
            PageUtils pageUtils = new PageUtils(memberStockList, total);
            return pageUtils;
		}

		List<MemberStockDO> memberStockList = memberStockService.list(query);
		int total = memberStockService.count(query);*/
		PageUtils pageUtils = new PageUtils(mapList, mapList.size());
		return pageUtils;
	}
	@ResponseBody
	@GetMapping("/getMemberStockHistoryList")
	@RequiresPermissions("wx:memberStock:memberStock")
	public PageUtils getMemberStockHistoryList(@RequestParam Map<String, Object> params){
		params.put("delFlag","0");
		params.put("sort","ca_at");
		params.put("order","desc");
		//查询列表数据
		//MemberStockDO memberStockDO = memberStockService.get(params.get("id").toString());
		/*params.put("goodsId", memberStockDO.getGoodsId());
		params.put("productId", memberStockDO.getProductId());
		params.put("userId", memberStockDO.getUserId());
		params.remove("id");*/
		Query query = new Query(params);
		List<MemberStockDO> list2 = memberStockService.list(query);
		
		 if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<MemberStockDO> memberStockList = memberStockService.likeList(query);
            List<Map<String, Object>> list = new ArrayList<>();
            if(memberStockList.size()>0){
            	for (MemberStockDO memberStockDO : memberStockList) {
            		Map<String, Object> entityToMap = StringUtils.entityToMap(memberStockDO);
            		OrderOrderDO orderOrderDO = orderOrderService.get(memberStockDO.getOrderId());
            		if(orderOrderDO!=null){
            			entityToMap.put("money", orderOrderDO.getPayment());
            			MemberUserDO memberUserDO = memberUserDao.get(orderOrderDO.getUserId());
            			if(memberUserDO!=null){
            				entityToMap.put("name", memberUserDO.getName());
            			}else{
            				entityToMap.put("name", "");
            			}
            		}else{
            			entityToMap.put("money", "");
            			entityToMap.put("name", "");
            		}
            		list.add(entityToMap);
				}
            }
            
            int total = memberStockService.count(params);
            PageUtils pageUtils = new PageUtils(list, total);
            return pageUtils;
		}
		 List<Map<String, Object>> list = new ArrayList<>();
         if(list2.size()>0){
         	for (MemberStockDO memberStockDO : list2) {
         		Map<String, Object> entityToMap = StringUtils.entityToMap(memberStockDO);
         		OrderOrderDO orderOrderDO = orderOrderService.get(memberStockDO.getOrderId());
         		if(orderOrderDO!=null){
         			entityToMap.put("money", orderOrderDO.getPayment());
         			MemberUserDO memberUserDO = memberUserDao.get(orderOrderDO.getUserId());
         			if(memberUserDO!=null){
         				entityToMap.put("name", memberUserDO.getName());
         			}else{
         				entityToMap.put("name", "");
         			}
         		}else{
         			entityToMap.put("money", "");
         			entityToMap.put("name", "");
         		}
         		list.add(entityToMap);
				}
         }
		int total = memberStockService.count(params);
		PageUtils pageUtils = new PageUtils(list2, total);
		return pageUtils;
	}
	
	@GetMapping("/add/{userId}")
	@RequiresPermissions("wx:memberStock:add")
	String add(@PathVariable("userId") String userId,Model model){
		model.addAttribute("userId", userId);
	    return "wx/memberStock/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberStock:edit")
	String edit(@PathVariable("id") String id,Model model){
		String[] split = id.split("&");
		
		MemberStockDO memberStock = new MemberStockDO();
		memberStock.setId(UuidUtil.get32UUID());
		memberStock.setGoodsId(split[0]);
		memberStock.setProductId(split[1]);
		memberStock.setDelFlag(0);
		memberStock.setUserId(split[2]);
		model.addAttribute("memberStock", memberStock);
	    return "wx/memberStock/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberStock:add")
	public R save( MemberStockDO memberStock){
	    memberStock.setId(UuidUtil.get32UUID());
	    memberStock.setDelFlag(0);
	    memberStock.setCaAt(new Date());
		if(memberStockService.save(memberStock)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberStock:edit")
	public R update( MemberStockDO memberStock){
		memberStock.setCause("后台管理员修改");
		MemberStockDO memberStockDO = memberStockService.get(memberStock.getId());
		if(memberStockDO==null){
			memberStock.setCaAt(new Date());
			memberStock.setUpAt(new Date());
			memberStockService.save(memberStock);
			return R.ok();
		}
		memberStockService.update(memberStock);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberStock:remove")
	public R remove( String id){
		MemberStockDO memberStock = memberStockService.get(id);
		memberStock.setDelFlag(1);
		memberStock.setUpAt(new Date());
		if(memberStockService.update(memberStock)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberStock:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MemberStockDO memberStock = memberStockService.get(string);
				memberStock.setDelFlag(1);
				memberStock.setUpAt(new Date());
				memberStockService.update(memberStock);
			}
		}
	
		return R.ok();
	}
	
}
